# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import os
import urllib
from znfls import settings

class ZnflsPipeline(object):
    def process_item(self, item, spider):
        dir_path = '%s/%s'%(settings.IMAGES_STORE, spider.name)
        if not os.path.exists(dir_path):
        	os.makedirs(dir_path)
        dir_name = '%s/%s'%(dir_path, item['title'])
        if not os.path.exists(dir_name):
        	os.makedirs(dir_name)
        for img_link in item['imglinks']:
        	list_name = img_link.split('/')
        	file_name = list_name[len(list_name)-1]
        	file_path = '%s/%s'%(dir_name, file_name)

        	if os.path.exists(file_name):
        		continue
        	with open(file_path, 'wb') as f:
        		conn = urllib.urlopen(img_link)
        		f.write(conn.read())
        	f.close()
        return item